package com.example.fang.demodruid.concurrency2;

public class MyTest4 {
    
    private Object lock1=new Object();

    private Object lock2=new Object();

    public  static void main(String[] args){
        MyTest4 myTest4=new MyTest4();
        Runnable runnable=()->{
             while (true){
                 myTest4.method1();
                 try {
                     Thread.sleep(100);
                 }catch (Exception e){
                     e.printStackTrace();
                 }
             }
        };
        Thread thread1=new Thread(runnable,"myThread1");
        Runnable runnable2=()->{
            while (true){
                myTest4.method2();
                try {
                    Thread.sleep(100);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        Thread thread2=new Thread(runnable2,"myThread2");
        thread1.start();
        thread2.start();

    }

    public void method1(){
        synchronized (lock1){
            synchronized (lock2){
                System.out.println("my method1 invoked");
            }
        }
    }
    public void method2(){
        synchronized (lock2){
            synchronized (lock1){
                System.out.println("my method2 invoked");
            }

        }
    }
}
